package com.ibeeking.found.msg.rest.manager;

import com.ibeeking.found.common.entity.msg.MsgSimpleParam;
import com.ibeeking.found.common.entity.msg.MsgTemplateParam;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.IMqService;
import com.ibeeking.found.msg.rest.service.IMsgBindConfigService;
import com.ibeeking.found.msg.service.common.param.MsgNoticeParam;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 发送消息manager
 *
 * @Author ibeeking
 */
@Component
public class SendMsgManager {
    @Resource
    private IMqService mqServiceImpl;

    /**
     * 发送一般消息通知
     * @param param
     * @return
     */
    public Boolean sendSimpleMessage(MsgSimpleParam param) {
        if(param == null) throw new BusinessException("读取参数对象为空！");
        if(StringUtils.isEmpty(param.getChannel()))  throw new BusinessException("消息类型参数不能为空！");
        String tenantId = param.getTenantId();
        if(StringUtils.isEmpty(tenantId)){
            tenantId = UserUtils.getTenantId();
            if(StringUtils.isEmpty(tenantId)) throw new BusinessException("读取租户Id为空！");
        }
        MsgNoticeParam msgNoticeParam = new MsgNoticeParam();
        msgNoticeParam.setTenantId(tenantId);
        msgNoticeParam.setChannel(param.getChannel());
        msgNoticeParam.setContent(param.getContent());
        mqServiceImpl.sendMsgNoticeMessage(msgNoticeParam);
        return true;
    }
    /**
     * 发送模板消息通知
     *
     * @param param
     * @return
     */
    public Boolean sendTemplateMessage(MsgTemplateParam param) {
        if (param !=null) {
            MsgNoticeParam msgNoticeParam = new MsgNoticeParam();
            msgNoticeParam.setTenantId(param.getTenantId());
            msgNoticeParam.setTemplateCode(param.getTemplateCode());
            msgNoticeParam.setContent(JsonUtils.toJsonStr(param));
            mqServiceImpl.sendMsgNoticeMessage(msgNoticeParam);
        }
        return true;
    }
}
